package com.lanou.beautifulwardrobe.Activity_zhuti.Adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.TimeManager;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ProductActivity;
import com.lanou.beautifulwardrobe.listener_interface.MyHotItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class ActivityHotRecyclerViewAdapter extends RecyclerView.Adapter<ActivityHotRecyclerViewAdapter.ActivityViewHolder> {

    private List<ProductActivity> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private MyHotItemClickListener listener;

    public void setOnMyHotItemClickListener(MyHotItemClickListener listener) {
        this.listener = listener;
    }


    public void addData(List<ProductActivity> productActivities) {
        datas = productActivities;
        notifyDataSetChanged();

    }

    @Override
    public ActivityViewHolder onCreateViewHolder(ViewGroup view, int viewType) {
        View v = LayoutInflater.from(view.getContext()).inflate(R.layout.activity_product_item, null);
        return new ActivityViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ActivityViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            ProductActivity productActivity = datas.get(position);
            holder.position = position;
            String imageUrl = productActivity.getPic_url();

            String title = productActivity.getTitle();
            String visits = productActivity.getVisits_count();
            String likes = productActivity.getLikes_count();
            String startTimeStr = productActivity.getStart_time();
            long startTime = Long.parseLong(startTimeStr);
            long endTime = Long.parseLong(productActivity.getEnd_time());
            holder.activity_productTitle.setText(title);
            holder.activity_productLooks.setText(visits);
            if (likes != null) {
                holder.activity_productLikes.setText(likes);
            } else {
                holder.activity_productLikes.setText("0");
            }

            holder.stratTime.setText(TimeManager.timeStamp2Date(startTimeStr, "yyyy年MM月dd日 HH时"));
            long current = Long.parseLong(TimeManager.timeStamp());
            if (current < endTime) {
                long time = endTime - current;
                long day = time / 86400;
                long hour = (time % 86400) / 3600;
                long min = ((time % 86400) % 3600) / 60;
                holder.activity_productTime.setText("剩余:" + day + "天" + hour + "时" + min + "分");
            } else {
                holder.activity_productTime.setText("已截止");
            }

            getBitmap(imageUrl, holder.activity_productImage);
        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class ActivityViewHolder extends RecyclerView.ViewHolder {
        private ImageView activity_productImage;
        private TextView activity_productTitle;
        private TextView activity_productTime;
        private TextView activity_productLooks;
        private TextView activity_productLikes;
        private TextView stratTime;
        private CardView cardview;
        private int position;

        public ActivityViewHolder(View view) {
            super(view);
            activity_productImage = (ImageView) view.findViewById(R.id.activity_productImage);
            activity_productTitle = (TextView) view.findViewById(R.id.activity_productTitle);
            activity_productTime = (TextView) view.findViewById(R.id.activity_productTime);
            activity_productLooks = (TextView) view.findViewById(R.id.activity_productLooks);
            activity_productLikes = (TextView) view.findViewById(R.id.activity_productLikes);
            stratTime = (TextView) view.findViewById(R.id.stratTime);
            cardview = (CardView) view.findViewById(R.id.cardview);
            cardview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (datas != null && datas.size() > 0) {
                        String url = datas.get(position).getUrl();
                        String title = datas.get(position).getShort_title();
                        String activity_type = datas.get(position).getActivity_type();
                        String ids=datas.get(position).getId();
                        if (listener != null) {
                            listener.onMyHotItemClickListener(url, title, activity_type,ids);
                        }
                    }
                }
            });
        }
    }

    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}
